package com.top.mall.custom

import android.content.Context
import android.graphics.Bitmap
import android.graphics.Color
import android.graphics.PorterDuff
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import android.text.Spannable
import android.text.SpannableString
import android.text.style.ImageSpan
import android.util.AttributeSet
import android.view.View
import android.widget.TextView
import androidx.appcompat.widget.AppCompatTextView
import com.blankj.utilcode.util.ConvertUtils
import com.top.mall.R

class TagTextView : AppCompatTextView {
    private var content_buffer: StringBuffer? = null
    private var tv_tag: TextView? = null
    private var mContext: Context

    //必须重写所有的构造器，否则可能会出现无法inflate布局的错误！
    constructor(context: Context) : super(context) {
        mContext = context
    }

    constructor(
        context: Context,
        attrs: AttributeSet?
    ) : super(context, attrs) {
        mContext = context
    }

    constructor(
        context: Context,
        attrs: AttributeSet?,
        defStyleAttr: Int
    ) : super(context, attrs, defStyleAttr) {
        mContext = context
    }

    fun setContentAndTag(
        content: String?,
        tags: List<String>,
        parseColor: String? = null
    ) {
        content_buffer = StringBuffer()
        for (item in tags) {
            content_buffer!!.append(item)
        }
        content_buffer!!.append(" ").append(content)
        val spannableString = SpannableString(content_buffer)
        for (i in tags.indices) {
            val item = tags[i]
            tv_tag = TextView(mContext).apply {
                textSize = 9f
                setTextColor(mContext.resources.getColor(R.color.colorWhite))
                setBackgroundResource(R.drawable.bg_gradients_red_flag)
                setPadding(
                    ConvertUtils.dp2px(4f),
                    ConvertUtils.dp2px(0f),
                    ConvertUtils.dp2px(4f),
                    ConvertUtils.dp2px(0f)
                )
                text = item
                if (!parseColor.isNullOrEmpty()) {
                    background.setColorFilter(
                        Color.parseColor(parseColor),
                        PorterDuff.Mode.DARKEN
                    )
                }
            }
            tv_tag?.let {
                val bitmap = convertViewToBitmap(it)
                val d: Drawable = BitmapDrawable(bitmap)
                d.setBounds(0, 0, it.width, it.height)
                val span = ImageSpan(d, ImageSpan.ALIGN_BASELINE)
                val endIndex: Int
                val startIndex: Int = getLastLength(tags, i)
                endIndex = startIndex + item.length
                spannableString.setSpan(span, startIndex, endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
            }
        }
        text = spannableString
        //        setGravity(Gravity.CENTER_VERTICAL);
    }

    private fun getLastLength(list: List<String>, maxLength: Int): Int {
        var length = 0
        for (i in 0 until maxLength) {
            length += list[i].length
        }
        return length
    }

    companion object {
        private fun convertViewToBitmap(view: View): Bitmap {
            view.measure(
                MeasureSpec.makeMeasureSpec(
                    0,
                    MeasureSpec.UNSPECIFIED
                ),
                MeasureSpec.makeMeasureSpec(
                    0,
                    MeasureSpec.UNSPECIFIED
                )
            )
            view.layout(0, 0, view.measuredWidth, view.measuredHeight)
            view.buildDrawingCache()
            return view.drawingCache
        }
    }
}